package cn.kgc83.shengyin.controller;

import cn.kgc83.shengyin.util.StrUtil;
import cn.kgc83.shengyin.vo.Result;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author oldliu
 * @since 1.0
 */
public class BaseController {
    @Autowired
    private RedisTemplate redisTemplate;
    public Object getLoginUser(){
        HttpServletRequest request =
                ((ServletRequestAttributes) (RequestContextHolder.currentRequestAttributes())).getRequest();
        String token = request.getHeader("Autho");
        Object user=token==null?null:redisTemplate.opsForValue().get(token);
        return user;
    }

    public void startPage(){
        //设置分页条件
        HttpServletRequest request =
                ((ServletRequestAttributes) (RequestContextHolder.currentRequestAttributes())).getRequest();
        int page= StrUtil.toInt(request.getParameter("page"),1);
        int limit= StrUtil.toInt(request.getParameter("limit"),10);
        Page<Object> objects = PageHelper.startPage(page, limit);

    }
    public <T> cn.kgc83.shengyin.vo.Page<T> getDataTable(List<T> datas){
        if(datas instanceof Page){
            Page<T> p1=(Page<T>)datas;

            return new cn.kgc83.shengyin.vo.Page<T>(p1.getResult(),p1.getTotal(),p1.getPageSize());
        }
        return new cn.kgc83.shengyin.vo.Page<T>(datas,datas.size(),datas.size());
    }

    public Result toAjax(int i){
        return i>0?Result.success():Result.error();
    }
    public Result success(Object o){
        return o!=null?Result.success(o):Result.error("对象不存在");
    }
}
